﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008-2009
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.IO

Imports Boogaloo.Core.BLL
Imports Boogaloo.Core.UI

Namespace UI.Webpages


    Partial Public Class BranchPages
        Inherits BaseAdminPage

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Try
                parent_node = Node.GetNode(CType(Request.QueryString("node"), Integer))
            Catch ex As Exception
                parent_node = Nothing
            End Try

            If Not Page.IsPostBack Then
                BindPage()
            End If

        End Sub


        Protected Sub BindPage()

            Dim nodes As List(Of Node) = Nothing
            If parent_node_id > 0 Then
                nodes = Node.GetChildNodes(parent_node_id)
            Else
                nodes = Node.GetNodesByLevelInTree(Groove.admin_root.root_id, 0)
            End If
            list_count = nodes.Count
            rpt_pages.DataSource = nodes
            rpt_pages.DataBind()

            If Not ShowSortFields() Then lb_sort.Visible = False

        End Sub


        Protected Sub rpt_pages_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_pages.ItemDataBound

            Dim n As Node = CType(e.Item.DataItem, Node)
            If n IsNot Nothing Then

                Dim hyp_page As HyperLink = CType(e.Item.FindControl("hyp_page"), HyperLink)
                hyp_page.NavigateUrl = String.Format("{0}/Designer.aspx?node={1}", Groove.path_admin_site, n.node_id.ToString)
                hyp_page.Text = n.menu_item_text


                Dim txt_sortorder As TextBox = CType(e.Item.FindControl("txt_sortorder"), TextBox)
                txt_sortorder.Text = n.sort_order

                Dim spn_icon As HtmlGenericControl = CType(e.Item.FindControl("spn_icon"), HtmlGenericControl)
                Dim nodisplay As String = String.Empty
                If n.display = False Then nodisplay = " nodisplay"
                Select Case n.node_type_id
                    Case NodeType.WebPage
                        spn_icon.InnerHtml = "Webpage"
                        spn_icon.Attributes.Add("class", String.Format("webpageNode{0}", nodisplay))
                    Case NodeType.Folder
                        spn_icon.InnerHtml = "Folder"
                        spn_icon.Attributes.Add("class", String.Format("folderNode{0}", nodisplay))
                    Case NodeType.ExternalUrl
                        spn_icon.InnerHtml = "External Url"
                        spn_icon.Attributes.Add("class", String.Format("externalUrlNode{0}", nodisplay))
                    Case NodeType.LocalFile
                        spn_icon.InnerHtml = "Local File"
                        spn_icon.Attributes.Add("class", String.Format("localFileNode{0}", nodisplay))
                End Select

                Dim lb_display As LinkButton = CType(e.Item.FindControl("lb_display"), LinkButton)
                lb_display.CommandName = "display"
                lb_display.CommandArgument = n.node_id
                If n.display Then
                    lb_display.Text = "Displays"
                    lb_display.CssClass = String.Empty
                Else
                    lb_display.Text = "Does not display"
                    lb_display.CssClass = "nodisplay"
                End If
                If n.is_default_node Then lb_display.Visible = False

                Dim lb_display_in_lists As LinkButton = CType(e.Item.FindControl("lb_display_in_lists"), LinkButton)
                lb_display_in_lists.CommandName = "display_in_lists"
                lb_display_in_lists.CommandArgument = n.node_id
                If n.display_in_lists Then
                    lb_display_in_lists.Text = "Displays in lists"
                    lb_display_in_lists.CssClass = String.Empty
                Else
                    lb_display_in_lists.Text = "Does not display in lists"
                    lb_display_in_lists.CssClass = "nodisplay"
                End If

                Dim lb_display_in_menus As LinkButton = CType(e.Item.FindControl("lb_display_in_menus"), LinkButton)
                lb_display_in_menus.CommandName = "display_in_menus"
                lb_display_in_menus.CommandArgument = n.node_id
                If n.display_in_menus Then
                    lb_display_in_menus.Text = "Displays in menus"
                    lb_display_in_menus.CssClass = String.Empty
                Else
                    lb_display_in_menus.Text = "Does not display in menus"
                    lb_display_in_menus.CssClass = "nodisplay"
                End If

                Dim lb_moveup As LinkButton = CType(e.Item.FindControl("lb_moveup"), LinkButton)
                lb_moveup.CommandName = "moveup"
                lb_moveup.CommandArgument = n.node_id
                If e.Item.ItemIndex <= 0 Then lb_moveup.Visible = False

                Dim lb_movedown As LinkButton = CType(e.Item.FindControl("lb_movedown"), LinkButton)
                lb_movedown.CommandName = "movedown"
                lb_movedown.CommandArgument = n.node_id
                If e.Item.ItemIndex = list_count - 1 Then lb_movedown.Visible = False

                If Not ShowSortFields() Then
                    txt_sortorder.Visible = False
                    lb_moveup.Visible = False
                    lb_movedown.Visible = False
                End If

            End If

        End Sub

        ''' <summary>
        ''' Handles all repeater linkbutton clicks
        ''' </summary>
        Protected Sub lb_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim lb As LinkButton = CType(sender, LinkButton)

            If lb IsNot Nothing Then
                Dim n As Node = Node.GetNode(CType(lb.CommandArgument, Integer))
                Select Case lb.CommandName
                    Case "display"
                        n.display = Not n.display
                        n.Update()
                    Case "display_in_lists"
                        n.display_in_lists = Not n.display_in_lists
                        n.Update()
                    Case "display_in_menus"
                        n.display_in_menus = Not n.display_in_menus
                        n.Update()
                    Case "moveup"
                        MoveSortUpDown(n)
                    Case "movedown"
                        MoveSortUpDown(n, False)
                End Select
                BindPage()
            End If

        End Sub

        Private Sub MoveSortUpDown(ByVal n As Node, Optional ByVal up As Boolean = True)
            Dim nodes As List(Of Node) = Nothing
            If n.parent_id = 0 Then ' this is the root level
                nodes = Node.GetNodesByLevelInTree(Groove.admin_root.root_id, 0)
            Else
                nodes = Node.GetChildNodes(n.parent_id)
            End If

            Dim found As Boolean = False
            Dim check As Node = Nothing
            For Each check In nodes
                If up Then
                    If check.sort_order + 1 = n.sort_order Then
                        check.sort_order = n.sort_order
                        n.sort_order = n.sort_order - 1
                        found = True
                    End If
                Else
                    If check.sort_order = n.sort_order + 1 Then
                        check.sort_order = n.sort_order
                        n.sort_order = n.sort_order + 1
                        found = True
                    End If
                End If
                If found Then
                    check.Update()
                    n.Update()
                    Exit For
                End If
            Next

        End Sub

        Protected Sub lb_sort_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_sort.Click

            Dim nodes As New List(Of Node)
            Dim sorts As New List(Of Integer)

            ' collect the nodes and new sorts from the repeater and synch lists
            Dim item As RepeaterItem = Nothing
            For Each item In rpt_pages.Items

                Dim n As Node = Node.GetNode(CType(item.FindControl("lb_display"), LinkButton).CommandArgument)
                If n IsNot Nothing Then
                    Dim sort As Integer = n.sort_order
                    Try
                        sort = CType(CType(item.FindControl("txt_sortorder"), TextBox).Text, Integer)
                    Catch ex As Exception
                        sort = n.sort_order
                    End Try

                    sorts.Add(sort)
                    nodes.Add(n)
                End If

            Next

            Node.SyncListSortOrderToList(nodes, sorts)

            BindPage()

        End Sub

       


        Private Function ShowSortFields() As Boolean

            If parent_node Is Nothing Then
                Return True
            ElseIf parent_node.child_sort_type = ChildSortType.ManualNodesInsertedFirst Or parent_node.child_sort_type = ChildSortType.ManualNodesInsertedLast Then
                Return True
            Else
                Return False
            End If

        End Function

        Public ReadOnly Property parent_node_id() As Integer
            Get
                If parent_node Is Nothing Then
                    Return 0
                Else
                    Return parent_node.node_id
                End If
            End Get
        End Property


        Private _parent_node As Node = Nothing
        Public Property parent_node() As Node
            Get
                Return _parent_node
            End Get
            Set(ByVal value As Node)
                _parent_node = value
            End Set
        End Property


        Private _list_count As Integer = 0
        Public Property list_count() As Integer
            Get
                Return _list_count
            End Get
            Set(ByVal value As Integer)
                _list_count = value
            End Set
        End Property

    End Class

End Namespace